<?php

namespace Apps\News\Controllers;

use Apps\News\Models\NewsModel;
use App\Controllers\FrontBase;

class Home extends FrontBase
{
    protected $newsModel;

    public function __construct(...$params)
    {
        $this->newsModel = new NewsModel();
    }

    public function index()
    {
        $catid = $this->request->getGet('catid');
        if (IS_POST) {
            $page = $this->request->getPost('page');
            $limit = $this->request->getPost('limit');
            $param = [];
            $param['catid'] = $catid ?? 0;
            list($list, $total, $param) = $this->newsModel->limit_page($page, 10, 0, $param);
            if ($list) {
                foreach ($list as $k => $v) {
                    $list[$k]['catname'] = $this->cats[$v['catid']]['name'];
                }
            }
            $return = [
                'total' => $total,
                'data' => $list,
            ];
            exit(ams_json(0, 'ok', $return));
        }
        $cats = $this->newsModel->getAllCats();
        $vdata = [
            'catid' => $catid,
            'cats' => $cats,
        ];
        return view('index.html');
        // return view('news/index.html', $vdata);
    }

    //分类
    public function cat()
    {
        $cats = $this->newsModel->getAllCats();
        if ($cats) {
            foreach ($cats as $k => $v) {
                $goods = $this->newsModel->getByCatid($v['id'], 6);
                $cats[$k]['goods'] = $goods;
            }
        }
        $vdata = [
            'cats' => $cats,
        ];
        return view('news/cat.html', $vdata);
    }

    //详情
    public function show()
    {
        $id = $this->request->getGet('id');
        $data = $this->newsModel->getById($id);
        $vdata = ['data' => $data];
        return view('news/show.html', $vdata);
    }

}
